package tutorial.java.swing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.security.Key;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

public class MenuDemo implements ActionListener{
	
	private JMenuItem miOpen;
	private JMenuItem miNew;
	private JMenuItem miSave;
	private JMenuItem miExit;
	
	public static void main(String[] args){
		new MenuDemo();
	}

	public MenuDemo(){
		JFrame frame = new JFrame("Menu Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu file = new JMenu("File");
		file.setMnemonic(KeyEvent.VK_V);
		menuBar.add(file);
		
		miNew = new JMenuItem("New");
		miNew.setMnemonic(KeyEvent.VK_N);
		miNew.addActionListener(this);
		file.add(miNew);
		
		miOpen = new JMenuItem("Open");
		miOpen.setMnemonic(KeyEvent.VK_O);
		miOpen.addActionListener(this);
		file.add(miOpen);
		
		miSave = new JMenuItem("Save");
		miSave.setMnemonic(KeyEvent.VK_S);
		miSave.addActionListener(this);
		file.add(miSave);
		
		file.addSeparator();
		
		miExit = new JMenuItem("Exit");
		miExit.setMnemonic(KeyEvent.VK_E);
		miExit.addActionListener(this);
		file.add(miExit);
		
		JMenu edit = new JMenu("Edit");
		edit.setMnemonic(KeyEvent.VK_E);
		menuBar.add(edit);
		
		JMenu miFind = new JMenu("Find");
		miFind.setMnemonic(KeyEvent.VK_F);
		edit.add(miFind);
		
		JMenuItem miUp = new JMenuItem("Up");
		miUp.setMnemonic(KeyEvent.VK_U);
		miFind.add(miUp);
		
		JMenuItem miDown = new JMenuItem("Down");
		miDown.setMnemonic(KeyEvent.VK_D);
		miFind.add(miDown);
		
		frame.setSize(500,400);
		frame.setVisible(true);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		JMenuItem item = (JMenuItem) e.getSource();
		if (item == miNew) {
			JOptionPane.showMessageDialog(null, "New is clicked");
		}
		if (item == miOpen) {
			JOptionPane.showMessageDialog(null, "Open is clicked");
		}
		if (item == miSave) {
			JOptionPane.showMessageDialog(null, "Save is clicked");
		}
		if (item == miExit) {
			System.exit(0);
		}
	}
}
